package com.example.demo.service.impl;

import com.example.demo.entity.Favorites;
import com.example.demo.mapper.FavoritesMapper;
import com.example.demo.service.IFavoritesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-05
 */
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements IFavoritesService {

    @Autowired
    private FavoritesMapper favoritesMapper;
    @Override
    public Integer addFavorites(Favorites favorites) {
        if (favorites.getUserId() < 0) {
            // 这里我选择了返回一个错误码 -1
            return -1;
        }
        List<Favorites> favoritesList  = favoritesMapper.selectFavoriteByUserAndArticle(favorites);
        if (favoritesList == null || favoritesList.size() < 1) {
            favoritesMapper.favoriteByUserAndArticle(favorites);
            return 1;
        }
        return 0;
    }

    @Override
    public Integer countfavoriteByArticle(Integer articleId) {
        return favoritesMapper.countfavoriteByArticle(articleId);
    }

    @Override
    public List<Favorites> selectAllFavorites() {
        return favoritesMapper.selectAllFavorites();
    }

    @Override
    public Integer deleteFavorites(Favorites favorites) {
        if (favoritesMapper.selectFavoriteByUserAndArticle(favorites) != null) {
            favoritesMapper.deleteFavorites(favorites);
            return 1;
        }
        return 0;
    }

    @Override
    public List<Favorites> selectFavorites(Favorites favorites) {
        return favoritesMapper.selectFavorites(favorites);
    }
}
